 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tld/importTag.tld" prefix="importTag"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@taglib uri="/WEB-INF/tld/pageutil.tld" prefix="pu" %>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<link rel="stylesheet" href="/css/jquery.autocomplete.css" type="text/css" media="screen" />

<c-rt:set var="frontContentHtmlPath" value='<%=ServerValue.getFrontContentHtmlPath()%>'/>
<c-rt:set var="hostName" value='<%=request.getServerName()%>'/>
${pu:reqSet('currentDate', pu:currentDate()) }
<script>
function submitSearchForm(){
	setTimeout('encodeKeywordFX()', 500);
}
function findValue(li){
	$('#keyword').attr('value', li.selectValue);
	$('#form_searchKeyword').attr('value', li.selectValue);
}
var hadKeyin=false;
$(function(){
	
	$('#keyword').autocomplete("/ecmall/search/listAutoComplete.do",
			{
				formatItem: function(row) {
					return "<a href=\"#\" onclick=\"submitSearchForm()\"><span>"+row[0]+"</span><em>约"+row[2]+"笔</em></a>";
   			 	},
   			 	//onItemSelect: findValue,
   			 	width:315,
   			 	scrollHeight:50000,
   			 	selectFirst:false
			}
			).click(function(){
				if($('#keyword').attr('value')!=''){
					$('.ac_results').show();
				}
			});
	
	if(getParameter('z')!=''){
		<c-rt:if test="${!empty param.z && param.z!='0'}">
		${pu:reqSet('searchCategory', pu:get('com.yaodian100.ec.category.entity.Category', pu:toLong(param.z)))}
	$('#z').attr('value', '${pu:split(searchCategory.categoryPath, '/')[0]}');
	</c-rt:if>
	<c-rt:if  test="${empty param.z && param.z=='0'}">
	$('#z').attr('value', '0');
	</c-rt:if>
	}

		
	
	<c-rt:choose>
	<c-rt:when test="${!empty param.z}">
		<%try{%>
		${pu:listAdd('kdParams', pu:toLong(param.z))}
		${pu:reqSet('theCate', param.z)}
		
		<%}catch(Exception e){}%>
	</c-rt:when>

	<c-rt:when test="${empty param.z && !empty productCategory}">
	${pu:listAdd('kdParams', pu:toLong(productCategory.id))}
	${pu:reqSet('theCate', productCategory.id)}
	
	
	</c-rt:when>
	<c-rt:otherwise>
	${pu:listAdd('kdParams', pu:toLong("0"))}
	${pu:reqSet('theCate', "0")}
	</c-rt:otherwise>
	</c-rt:choose>
	${pu:reqSet('hotTerms', pu:queryByHql('from Keyword where categoryId=?', pu:arrayGet('kdParams'), null ))}
	${pu:reqSet('kds', pu:queryByHql('from KeywordDefault where categoryId=?', pu:arrayGet('kdParams'), null ))}
	<c-rt:if test="${empty hotTerms}">
	${pu:reqSet('kdParams', null)}
	${pu:listAdd('kdParams', pu:toLong("0"))}
	${pu:reqSet('hotTerms', pu:queryByHql('from Keyword where categoryId=?', pu:arrayGet('kdParams'), null ))}
	</c-rt:if>
	<c-rt:if test="${empty kds}">
	${pu:reqSet('kdParams', null)}
	${pu:listAdd('kdParams', pu:toLong("0"))}
	${pu:reqSet('kds', pu:queryByHql('from KeywordDefault where categoryId=?', pu:arrayGet('kdParams'), null ))}
	</c-rt:if>
});
var kd='${kds[0].keyword}';
var clicked=false;
/* 
function encodeKeywordFX(){
	if(hadKeyin){
		encodeKeyword();
	}else if(document.searchForm.z.value!='0'){
		document.searchForm.searchKeyword.value='';
		encodeKeyword();
	}else{
		location.href='/ecmall/search/advsearch.do';
	}
	return false;
}
*/
function encodeKeywordFX(){
	if(hadKeyin || document.searchForm.z.value!='0'){
		if(!hadKeyin){
			document.searchForm.searchKeyword.value='';
		}
		encodeKeyword();
	}else{
		location.href='/ecmall/search/advsearch.do';
	}
}
</script>
<c-rt:if test="${!empty param.z}">
${pu:reqSet('theCate', param.z) }
</c-rt:if>
<div id="searchDialog"></div>
<div class="search-bar"> 


  <form action="javascript:void(0);" name="searchForm" id="searchForm" > 
    <input type="text" id="keyword" name="searchKeyword" class="search-dpage" value="" autocomplete="off" onkeydown="javascript:if(event.keyCode==13){encodeKeywordFX();}" onkeypress="javascript:if(event.keyCode==13){encodeKeywordFX()}"/> 
    <input type="hidden" id="catLevel" name="catLevel" value="-1"/>
    <input type="hidden" name="searchWithBrief" value="0"/>
	<input type="hidden" name="searchWithDescription" value="0"/>
	<input type="hidden" name="fuzzySearch" value="${param.fuzzySearch }"/>
	<input type="hidden" name="brandId" value="${param.brandId }"/>
	${pu:reqSet('catListHql', 'from Category where level=? and isDelete=? and isVisible=? and startDate<=? and endDate>=? order by showSeq asc') }
	${pu:listAdd('catListParams', '0') }
	${pu:listAdd('catListParams', 'N') }
	${pu:listAdd('catListParams', 'Y') }
	${pu:listAdd('catListParams', currentDate) }
	${pu:listAdd('catListParams', currentDate) }
	${pu:reqSet('catList', pu:queryByHql(catListHql, pu:arrayGet('catListParams'), null)) }
    <select id="z" name="z" class="sel-page z"> 
	<option value="0" selected>全站</option> 	
	<c-rt:forEach items="${catList }" var="catTop">
	<option value="${catTop.id }">${catTop.name }</option>
	</c-rt:forEach>
	</select> 
    <a href="#" onclick="traceVirtualPage('/search.do?hpp=seach');encodeKeywordFX();" class="btn-serach"><span>搜索</span></a> <a href="/ecmall/search/advsearch.do?hpp=seach_h" class="search-more">高级搜索</a> 
  </form> 
  <div class="hot-keywd"> 
    <strong class="hottitle-wrap"><span class="hottitle">热门：</span></strong> 
    <span id="hotTermZone">
    <ul> 
    <c-rt:if test="${!empty theCate && theCate!='0'}">
    <%try{ %>
    ${pu:reqSet('theCategory', pu:get('com.yaodian100.ec.category.entity.Category', theCate)) }
    <%}catch(Exception e){} %>
    </c-rt:if>
    ${pu:reqSet('hotWord', hotTerms[0].word) }
    <%
    String hotWord=(String)request.getAttribute("hotWord");
    if(hotWord!=null){
    String[] termSplit=hotWord.split("\n");
    request.setAttribute("termSplit", termSplit);
    }
    %>
    <c-rt:forEach items="${termSplit}" var="hotTerm">
      <c-rt:set var="theCat" value="${theCate}" scope="request"/>
      ${pu:reqSet('theCateLevel', theCategory.level) }
      <c-rt:if test="${empty theCategory}">
      <c-rt:set var="theCat" value="" scope="request"/>
      ${pu:reqSet('theCateLevel', '-1') }
      </c-rt:if>
      
      
      <li><a href="#" onclick="traceVirtualPage('/search.do?hpp=Hotwd');document.searchForm.catLevel.value='${theCateLevel }';document.searchForm.z.value='${theCate }';encodeKeyword('${hotTerm}');return false;">${hotTerm }</a></li> 
    </c-rt:forEach>
    </ul>
    </span> 
  </div> 
<script>
$('#keyword').attr('value', '${pu:urlDecoding(param.searchKeyword, 'UTF-8')}');
<c-rt:if test="${empty param.searchKeyword && !empty kds}">

<%--
$('input[name=searchKeyword]').each(function(){
	$(this).attr('value', '${kds[0].keyword}');
	$(this).blur(function(){
		if($(this).attr('value')==''){
			$('input[name=searchKeyword]').each(function(){
			$(this).attr('value', '${kds[0].keyword}');
			});
		}
	});
	$(this).click(function(){
		if($(this).attr('value')=='${kds[0].keyword}'){
		$(this).attr('value', '');	
		}
	});
});
--%>

$('#keyword').attr('value', '${kds[0].keyword}');
$('#keyword').blur(function(){
	if($(this).attr('value')==''){
		$('#keyword').attr('class', 'search-dpage');
		$(this).attr('value', '${kds[0].keyword}');
		hadKeyin=false;
	}else if($(this).attr('value')!=kd){
		hadKeyin=true;
	}
});
$('#keyword').click(function(){
	$('#keyword').attr('class', 'search-page');
	if(!hadKeyin){
	//if($(this).attr('value')=='${kds[0].keyword}'){
	$(this).attr('value', '');	
	//}
	}

});

</c-rt:if>
function checkForEnter(event) {
	if (event.keyCode == 13) {
		setTimeout('encodeKeywordFX()', 500);
		return false;
	}
}

if(getParameter('searchKeyword')!=''){
$('#keyword').attr('class', 'search-page');
hadKeyin=true;
}
$('#keyword').keyup(function(){
	$('#form_searchKeyword').attr('value', $('#keyword').attr('value'));
	if($('#keyword').attr('value')!=''){
		hadKeyin=true;
		$('#relatedSort').show();
	}else{
		hadKeyin=false;
		$('#relatedSort').hide();
	}
});
/*
$('#keyword').change(function(){
	$('#form_searchKeyword').attr('value', $('#keyword').attr('value'));
	if($(this).attr('value')!='' && $(this).attr('value')!=kd){
		hadKeyin=true;
		$('#relatedSort').show();
	}else{
		hadKeyin=false;
		$('#relatedSort').hide();
	}
});
*/

</script>
</div> 
